package com.fjnldx.ks.service;

import com.fjnldx.ks.mapper.PowerMapper;
import com.fjnldx.ks.pojo.domain.SchedulingShift;
import com.fjnldx.ks.pojo.enums.GXEnum;
import com.fjnldx.ks.pojo.request.SchedulingShiftInModel;
import com.fjnldx.ks.pojo.response.Result;
import com.fjnldx.ks.utils.Common;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class PowerService {

    @Resource
    PowerMapper powerMapper;
    @Resource
    Common common;


    /**
     * 查询当班某工序各品种电耗
     *
     * @return
     */
    public Result queryPowerOfVarietyOnDuty(SchedulingShiftInModel params) {
        List<SchedulingShift> schedulingShifts = common.querySchedulingShift(params);
        if (schedulingShifts.size() > 0) {
            SchedulingShift shift = schedulingShifts.get(0);
            LocalDateTime startTime = shift.getStartTime();
            LocalDateTime endTime = shift.getEndTime();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            params.setStartTime(formatter.format(startTime));
            params.setEndTime(formatter.format(endTime));
            List<Map> result;

            String gx = params.getGongxu();
            if (GXEnum.Xs.getVal().equals(gx) || GXEnum.Lt.getVal().equals(gx)) {
                result = powerMapper.queryPowerOfVarietyOnDuty(params);
            }else {
                return Result.error("暂不支持该工序查询");
            }
            return Result.success(result);
        } else {
            return Result.error("请前往轮班管理安排当日班次计划");
        }
    }


}
